//
//  VIZAddColockCell.m
//  Young-Old
//
//  Created by 位立志 on 16/4/29.
//  Copyright © 2016年 位立志. All rights reserved.
//

#import "VIZAddColockCell.h"

@implementation VIZAddColockCell

- (void)awakeFromNib {

    
}

//设置字典item
- (void)setItem:(NSDictionary *)item{
    
    _item = item;
    //头
    self.textLabel.text = item[VIZTitle];
    
    //副标题
//    if ([item[VIZSubtitle] length]>0) {
//        self.detailTextLabel.text = item[VIZSubtitle];
//    }
        
    //尾视图类型
    if ([item[VIZAccessoryType] length]>0) {
        
        NSString *typeStr = item[VIZAccessoryType];
        if ([typeStr isEqualToString:@"UITableViewCellAccessoryDisclosureIndicator"]) {
            self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
    }
    
    //尾部视图
    if ([item[VIZAccessoryView] length]>0) {
        NSString *viewStr = item[VIZAccessoryView];
        if ([viewStr isEqualToString:@"UISwitch"]) {
            UISwitch *s = [[UISwitch alloc]init];
            
#pragma mark - 读switch
            
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            BOOL isOn = [userDefault objectForKey:self.item[VIZKeyName]];
            s.on = isOn;
            self.accessoryView = s;
            
            [s addTarget:self action:@selector(sValueChanged:) forControlEvents:UIControlEventValueChanged];
            
        }

    }
    
}

//switch改变的时候执行的方法
- (void)sValueChanged:(UISwitch*)s{
    
    //存数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setBool:s.on forKey:self.item[VIZKeyName]];
    
    [userDefault synchronize];
    
}




/*

 //使用偏好设置(存储数据)
 - (IBAction)btnClick {
 //   1,获取Default对象,通过这个对象来存储数据
 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
 //    2.通过这个对象来存储数据
 [userDefault setObject:@"zhangsan" forKey:@"name"];
 [userDefault setObject:@"163.com" forKey:@"email"];
 [userDefault setBool:YES forKey:@"gender"];
 //    3.立即同步
 [userDefault synchronize];
 
 }
 
 
 - (void)readData{
 //    1.获取Default对象
 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
 //    2.通过这个对象来读取数据
 NSString *name =  [userDefault objectForKey:@"name"];
 NSString *email = [userDefault objectForKey:@"email"];
 BOOL gender = [userDefault boolForKey:@"gender"];
 ////    3.同步
 //    [userDefault synchronize];
 NSLog(@"%@----%@---%d",name,email,gender);
 
 
 }
 

 
 */




//创建cell的方法
+ (instancetype)cellWithTableView:(UITableView *)tableView andItem:(NSDictionary *)item{
    
    static NSString *ID = @"cell";
    
    VIZAddColockCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:[self cellStyleWithItem:item] reuseIdentifier:ID];
    }
    
    return cell;
}




#pragma mark - 根据cell自己样式字符串返回枚举值
+ (UITableViewCellStyle)cellStyleWithItem:(NSDictionary *)item {
    
    // 定义一个变量保存枚举值
    UITableViewCellStyle style = 0;
    if ([item[VIZTableViewCellStyle] isEqualToString:@"UITableViewCellStyleSubtitle"]) {
        
        style = UITableViewCellStyleSubtitle;
        
    } else if ([item[VIZTableViewCellStyle] isEqualToString:@"UITableViewCellStyleValue1"]) {
        
        style = UITableViewCellStyleValue1;
        
    }else if ([item[VIZTableViewCellStyle] isEqualToString:@"UITableViewCellStyleValue2"]) {
        
        style = UITableViewCellStyleValue2;
        
    } else {
        
        style = UITableViewCellStyleDefault;
    }
    return style;
    
}






//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//
//}

@end
